<?php
namespace application\cms\controller\admin;

class ArticleSort{

    private $rs_sort;
    
    function __construct()
    {
        //查询文章分类列表 select
        $sql="select * from ".tablename('rj_cms_article_sort')." order by parent_path,list_order ";
        $this->rs_sort=$GLOBALS['db']->query($sql);
    }

    //文章分类首页
    function index(){   

        //查询文章分类列表
        $sql="select * from ".tablename('rj_cms_article_sort');
        if(isset($_REQUEST['keyword']) && $_REQUEST['keyword']!='' ){
            $keyword = trim($_REQUEST['keyword']);
            $sql .= " where sort_name like '%$keyword%' ";
        }
        $sql.=" order by parent_path,list_order ";

        //$page=new \library\deep\Page(20);
        $page= \library\deep\Page::new_instance();

        $sql=$page->getPageSQL( $sql , 20 );

        $rs=$GLOBALS['db']->query($sql);
        
        require view('admin/article/article_sort.tpl.php');
    } 

    //添加文章分类页面
    function article_sort_add(){
        require view('admin/article/article_sort_add.tpl.php');
    }

    //修改文章分类页面
    function article_sort_edit(){
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="select * from ".tablename('rj_cms_article_sort')." where sort_id=".$id;
            $ret=$GLOBALS['db']->query($sql)->fetch();
        }else{
            message('未找到该文章分类,请刷新页面',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
        }
        require view('admin/article/article_sort_edit.tpl.php');
    }

    //保存
    function article_sort_save(){
        if(!isset($_POST['sort_name']) && $_POST['sort_name']==''){
            message('文章分类名称不能为空',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
        }

        $data = [
            'parent_id' => $_POST["parent_id"] ,
            'sort_name' => $_POST["sort_name"] ,
            'place' => $_POST["place"] ,
            'sort_img' => isset($_POST['sort_img'])?$_POST['sort_img']:'' ,
            'tip' => $_POST["tip"] ,
            'list_order' => $_POST["list_order"] ,
            'tpl_name' => $_POST["tpl_name"] ,
            'status' => $_POST["status"] ,
        ];

        //修改
        if( isset($_POST['id']) && $_POST['id']!=''){
            
            //条件
            $where['sort_id']= intval($_POST['id']);

            if($data['parent_id']==0){
                $data['parent_path'] = '0,' . $where['sort_id'] . ',';	
            }else{
                $sql="select parent_path from ".tablename('rj_cms_article_sort')." where sort_id= " . $data['parent_id'];// 父路径
                $data['parent_path'] = $GLOBALS['db']->query($sql)->fetchColumn() ;
                $data['parent_path'] = $data['parent_path']  . $where['sort_id'] . ',';
            }

            // 子类父路径批量修改
            $sql="select parent_path from ".tablename('rj_cms_article_sort')." where sort_id= " . $where['sort_id'];// 
            $ret_parent_path = $GLOBALS['db']->query($sql)->fetchColumn();
            $sql="select * from ".tablename('rj_cms_article_sort')." where parent_path like '$ret_parent_path%' " ;// 
            $rs_parent_path = $GLOBALS['db']->query($sql);
            foreach($rs_parent_path as $row){
                $tmp = str_replace( $ret_parent_path , $data['parent_path'] , $row['parent_path'] );
                pdo_update('rj_cms_article_sort',['parent_path'=> $tmp],['sort_id'=>$row['sort_id']]);
            }
            
           
            //执行sql
            $ok = pdo_update('rj_cms_article_sort',$data,$where);
            if($ok){
                message('修改成功',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
            }else{
                message('修改失败',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
            }
        
        //添加
        }else{
            
            //执行sql
            $ok = pdo_insert('rj_cms_article_sort',$data);
            if($ok){
                $sort_id=$GLOBALS['db']->lastInsertId();
                if($data['parent_id']==0){
                    $parent_path = "0,$sort_id,";	
                }else{
                    $sql="select parent_path from ".tablename('rj_cms_article_sort')." where sort_id= " . $data['parent_id'];// 父路径
                    $parent_path = $GLOBALS['db']->query($sql)->fetchColumn() ;
                    $parent_path = $parent_path . $sort_id . ',';
                }
                pdo_update('rj_cms_article_sort',['parent_path'=>$parent_path],['sort_id'=>$sort_id]);
                
                message('添加成功',url([ _MODULE_NAME_ ,'admin/articlesort','article_sort_add']));
            }else{
                message('添加失败',url([ _MODULE_NAME_ ,'admin/articlesort','article_sort_add']));
            }
        }
    }
    
    //删除文章分类
    function article_sort_delete(){
        
        if( isset($_GET['id']) && $_GET['id']!='' ){
            $id = intval( $_GET['id'] );
            $sql="delete from ".tablename('rj_cms_article_sort')." where sort_id=".$id;
            $ok=$GLOBALS['db']->exec($sql);
            if($ok){
                message('删除成功',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
            }else{
                message('删除失败',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
            }
        }else{
            message('未找到该文章分类,请刷新页面',url([ _MODULE_NAME_ ,'admin/articlesort','index']));
        }
    }

}
